package paolo.android.bookloc;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Location {
	private String name = "";
	private String address = "";
	private String type = "";
	private String notes = "";
	
	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public String toString() {
		return getName();
	}
	
	void save(SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		
		cv.put("name", name);
		cv.put("address", address);
		cv.put("type", type);
		cv.put("notes", notes);
		db.insert("locations", "name", cv);
	}
	
	static Cursor getAll(SQLiteDatabase db, String orderBy) {
		return db.rawQuery("SELECT * FROM locations " + orderBy, null);
	}
	
	Location loadFrom(Cursor c) {
		name = c.getString(c.getColumnIndex("name"));
		address = c.getString(c.getColumnIndex("address"));
		type = c.getString(c.getColumnIndex("type"));
		notes = c.getString(c.getColumnIndex("notes"));
		
		return this;
	}
	
	static Location getById(String id, SQLiteDatabase db) {
		String[] args = {id};
		Cursor c = db.rawQuery("SELECT * FROM locations WHERE _id=?", args);
		c.moveToFirst();
		
		Location result = new Location().loadFrom(c);
		c.close();
		
		return result;
	}
	
	void update(String id, SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		String[] args = {id};
		
		cv.put("name", name);
		cv.put("address", address);
		cv.put("type", type);
		cv.put("notes", notes);
		db.update("locations", cv, "_id=?", args);
	}

}
